Now that we've learned about NumPy let's test your knowledge. We'll start off with a few simple tasks and then you'll be asked some more complicated questions.
IMPORTANT NOTE! Make sure you don't run the cells directly above the example output shown, otherwise you will end up writing over the example output!
In [1]:
import numpy as np
In [2]:
# CODE HERE
np.zeros(10)
Out[2]:
In [3]:
# CODE HERE
np.ones(10)
Out[3]:
In [4]:
# CODE HERE
np.ones(10) * 5
Out[4]:
In [5]:
# CODE HERE
np.arange(10, 51)
Out[5]:
In [6]:
# CODE HERE
np.arange(10, 51, 2)
Out[6]:
In [7]:
# CODE HERE
np.arange(9).reshape(3,3)
Out[7]:
In [8]:
# CODE HERE
np.eye(3)
Out[8]:
In [9]:
# CODE HERE
np.random.randn(1)
Out[9]:
In [10]:
# CODE HERE
np.random.randn(25)
Out[10]:
In [11]:
np.arange(1, 101).reshape(10, 10) / 100
Out[11]:
In [12]:
np.linspace(0, 1, 20)
Out[12]:
In [13]:
# HERE IS THE GIVEN MATRIX CALLED MAT
# USE IT FOR THE FOLLOWING TASKS
mat = np.arange(1,26).reshape(5,5)
mat
Out[13]:
In [14]:
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
In [15]:
mat[2:, ]
Out[15]:
In [16]:
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
In [17]:
mat[3, -1]
Out[17]:
In [18]:
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
In [19]:
mat[:3, 1].reshape(3, 1)
Out[19]:
In [20]:
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
In [21]:
mat[-1, :]
Out[21]:
In [22]:
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
In [23]:
mat[-2:, :]
Out[23]:
In [24]:
# CODE HERE
np.sum(mat)
Out[24]:
In [25]:
# CODE HERE
np.std(mat)
Out[25]:
In [26]:
# CODE HERE
np.sum(mat, axis = 0)
Out[26]:
We worked a lot with random data with numpy, but is there a way we can insure that we always get the same random numbers? Click Here for a Hint
In [27]:
# My favourite number is 7
np.random.seed(7)